package com.lb.jwitter.service;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.jws.WebService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.*;
import com.lb.jwitter.hibernate.*;
import com.lb.jwitter.util.ChumanoLogger;
import com.lb.jwitter.util.ConditionReport;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

@Service("tbl_DKMH_DotDK_HocVien_ChiTietService")
@Transactional
@WebService(endpointInterface = "com.lb.jwitter.service.Tbl_DKMH_DotDK_HocVien_ChiTietService")
public class Tbl_DKMH_DotDK_HocVien_ChiTietServiceImpl implements
		Tbl_DKMH_DotDK_HocVien_ChiTietService, Serializable {

	private Log log = LogFactory.getLog(Tbl_DKMH_DotDK_HocVien_ChiTietServiceImpl.class);
	private static final long serialVersionUID = -8740095022264495229L;

	@Autowired
	@Qualifier("tbl_DKMH_DotDK_HocVien_ChiTietDAO")
	private Tbl_DKMH_DotDK_HocVien_ChiTietDAO _tbl_DKMH_DotDK_HocVien_ChiTietDao;

	@Autowired
	@Qualifier("tbl_ThongTin_HocVienDAO")
	private Tbl_ThongTin_HocVienDAO _tbl_ThongTin_HocVienDao;

	@Autowired
	@Qualifier("tbl_DKMH_DotDK_HocVienDAO")
	private Tbl_DKMH_DotDK_HocVienDAO _tbl_DKMH_DotDK_HocVienDao;

	@Autowired
	@Qualifier("tbl_DKMH_DotDK_NhomDAO")
	private Tbl_DKMH_DotDK_NhomDAO _tbl_DKMH_DotDK_NhomnDao;
	
	public final List<Tbl_DKMH_DotDK_HocVien_ChiTiet> findAlltbl_DKMH_DotDK_HocVien_ChiTiet(
			int pageSize, int page) {
		List<Tbl_DKMH_DotDK_HocVien_ChiTiet> result = get_tbl_DKMH_DotDK_HocVien_ChiTietDao()
				.findAll(pageSize, page);
		log.info("Number of user in DB:: " + result.size());
		return result;
	}

	@Transactional(noRollbackFor = IllegalArgumentException.class)
	public final Long createtbl_DKMH_DotDK_HocVien_ChiTiet(
			Tbl_DKMH_DotDK_HocVien_ChiTiet obj) {
		return _tbl_DKMH_DotDK_HocVien_ChiTietDao.save(obj);
	}

	public final void updatetbl_DKMH_DotDK_HocVien_ChiTiet(
			Tbl_DKMH_DotDK_HocVien_ChiTiet obj) {
		_tbl_DKMH_DotDK_HocVien_ChiTietDao.update(obj);
	}// end update methode

	public final Tbl_DKMH_DotDK_HocVien_ChiTiet gettbl_DKMH_DotDK_HocVien_ChiTiet(
			Long id) {
		log.info("Try to retrieve the tbl_DKMH_DotDK_HocVien_ChiTiet: " + id);
		return get_tbl_DKMH_DotDK_HocVien_ChiTietDao().get(id);
	}

	public void deletetbl_DKMH_DotDK_HocVien_ChiTiet(Long id) {
		log.info("Try to delete the tbl_DKMH_DotDK_HocVien_ChiTiet by Id : "
				+ id);
		if (_tbl_DKMH_DotDK_HocVien_ChiTietDao.exists(id)) {
			_tbl_DKMH_DotDK_HocVien_ChiTietDao.delete(id);
		} else {
			throw new IllegalArgumentException("EmployeeId '" + id
					+ "' does not exists");
		}
	}

	public void deleteListtbl_DKMH_DotDK_HocVien_ChiTiet(String[] listId) {
		for (String id : listId) {
			deletetbl_DKMH_DotDK_HocVien_ChiTiet(Long.parseLong(id));
		}
	}

	public String createListtbl_DKMH_DotDK_HocVien_ChiTietByJson(int dotDKId,
			String data, String ngayBatDauStr, String ngayKetThucStr,
			String quyen,
			Tbl_TaiKhoan taiKhoan, int NhomId) {
		String reString = "";

		//tim nhom
		Tbl_DKMH_DotDK_Nhom nhom = findNhom(NhomId);
		if(nhom==null) {
			ChumanoLogger.log("0Khong co nhom");
			ChumanoLogger.stop();
			reString ="Khong tim duoc nhom";
			return reString;
		}

		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		Date ngayBatDau;
		Date ngayKetThuc;
		try {
			ngayBatDau = df.parse(ngayBatDauStr);
		} catch (Exception e) {
			reString += "Nga\u0300y b\u0103\u0301t \u0111\u00E2\u0300u kh\u00F4ng h\u01A1\u0323p l\u00EA\u0323!!!";

			return reString;
		}
		try {
			ngayKetThuc = df.parse(ngayKetThucStr);

		} catch (Exception e) {
			reString += " Nga\u0300y k\u00EA\u0301t thu\u0301c kh\u00F4ng h\u01A1\u0323p l\u00EA\u0323!!!";

			return reString;
		}
		// -----------------------------------------------------------------------------
		try {
			ChumanoLogger.init(Tbl_DKMH_DotDK_HocVien_ChiTietServiceImpl.class.getName());
			ChumanoLogger.log("0BAT DAU INSERT DU LIEU");
			
			List<Tbl_DKMH_DotDK_HocVien_ChiTiet> tbl_DKMH_DotDK_HocVien_ChiTiets = new ArrayList<Tbl_DKMH_DotDK_HocVien_ChiTiet>();
			JSONArray jsons = (JSONArray) JSONSerializer.toJSON(data);

			for (int i = 0; i < jsons.size(); i++) {
				String logStr = "";
				JSONObject obj = jsons.getJSONObject(i);
				// --------------------------------------
				// Lay du lieu
				//MSSV
				//KHONGDANGKYYN
				//KHONGDANGKYLYDO
				//SOTINCHIDANGKY
				String mssv = obj.getString("MSSV");
				Boolean khongDK = true;
				String str = obj.getString("KHONGDANGKY");
				if(str==null || str.isEmpty()){
					khongDK = false;
				}
				String khongDKLyDo = obj.getString("KHONGDANGKYLYDO");
				str = obj.getString("SOTINCHIDANGKY");
				int sotinchi = 0;
				if(str!=null && !str.isEmpty()){
					sotinchi = Integer.parseInt(obj.getString("SOTINCHIDANGKY"));
				}
				
				
				logStr = "[ " + (i + 1) + " - " 
						+ "MSSV: " + mssv + " - "
						+ "KHONGDK: " + khongDK + " - " 
						+ "KHONGDKLyDo: "+ khongDKLyDo+ " - " 
						+ "SOTINCHI: "+ sotinchi+ " - " 
						+ "QUYEN: "+ quyen
						+ "]";
				
				//========================================
				//tim hoc vien
				List<Tbl_ThongTin_HocVien> hocVien = findHocVien(mssv);
				if(hocVien==null||hocVien.size()==0){
					ChumanoLogger.log(logStr+" NOT MSSV");
					continue;
				}
				
				//tim dotdk hocvien
				List<Long> dotDkHocVienId = findDotDKHocVienId(mssv, dotDKId);
				
				//-----------------------------------------
				//=========================================
				//Lay thong tin dotdk hocvien
				Tbl_DKMH_DotDK_HocVien tblDKMHDotDKHocVien = new Tbl_DKMH_DotDK_HocVien();
				//dotdk
				Tbl_DKMH_DotDK tblDKMHDotDK = new Tbl_DKMH_DotDK();
				tblDKMHDotDK.setId(new Long(dotDKId));
				tblDKMHDotDKHocVien.setDotDKId(tblDKMHDotDK);
				//hocvien
				tblDKMHDotDKHocVien.setHocVienId(hocVien.get(0));
				//set Can Bo
				tblDKMHDotDKHocVien.setCanBoId(taiKhoan.getCanBoId());
				tblDKMHDotDKHocVien.setKhongDangKyLyDo(khongDKLyDo);
				tblDKMHDotDKHocVien.setKhongDangKyYN(khongDK);
				
				//so tin chi
				tblDKMHDotDKHocVien.setSoTCDangKyToiDa(sotinchi);
				//set ngay cap nhat
				tblDKMHDotDKHocVien.setNgayCapNhat(new Date());
				
				//-------------------
				if (dotDkHocVienId != null && dotDkHocVienId.size() > 0) {
					tblDKMHDotDKHocVien.setId(dotDkHocVienId.get(0));
					_tbl_DKMH_DotDK_HocVienDao.update(tblDKMHDotDKHocVien);
					
				} else {
					//tao moi dot dk hoc vien
					Long dotDKHVId = _tbl_DKMH_DotDK_HocVienDao.save(tblDKMHDotDKHocVien);
					tblDKMHDotDKHocVien.setId(dotDKHVId);
					logStr+= " NEW DotDK_HOCVIEN";
				}
				
				//--------------------
				if(!khongDK){ // NEU duoc dang ky
					//tao moi dot dk hocvien chitiet
					Tbl_DKMH_DotDK_HocVien_ChiTiet tbl_DKMH_DotDK_HocVien_ChiTiet = new Tbl_DKMH_DotDK_HocVien_ChiTiet();
					tbl_DKMH_DotDK_HocVien_ChiTiet.setDotDKHocVienId(tblDKMHDotDKHocVien);
					tbl_DKMH_DotDK_HocVien_ChiTiet.setNgayBatDau(ngayBatDau);
					tbl_DKMH_DotDK_HocVien_ChiTiet.setNgayKetThuc(ngayKetThuc);
					tbl_DKMH_DotDK_HocVien_ChiTiet.setQuyen(quyen);

					tbl_DKMH_DotDK_HocVien_ChiTiet.setNhomDKId(nhom);

	
					tbl_DKMH_DotDK_HocVien_ChiTiets.add(tbl_DKMH_DotDK_HocVien_ChiTiet);
				}
				
				ChumanoLogger.log(logStr);
			} //end for
			
			_tbl_DKMH_DotDK_HocVien_ChiTietDao.saveOrUpdateAll(tbl_DKMH_DotDK_HocVien_ChiTiets);
			
		} catch (Exception e) {
			ChumanoLogger.log("ERROR: " + e.getMessage());
			System.out.println("------------ERROR CHUMANO-------------");
			e.printStackTrace();
			System.out.println("=================================");
		}
		ChumanoLogger.stop();

		return reString;
	}

	public List<Tbl_DKMH_DotDK_HocVien_ChiTiet> findtbl_DKMH_DotDK_HocVien_ChiTietByCondition(
			List<ConditionReport> cons, int pageSize, int page) {
		return _tbl_DKMH_DotDK_HocVien_ChiTietDao.findByCondition(
				Tbl_DKMH_DotDK_HocVien_ChiTiet.class, cons, pageSize, page);
	}

	public int counttbl_DKMH_DotDK_HocVien_ChiTietByCondition(
			List<ConditionReport> cons) {
		return _tbl_DKMH_DotDK_HocVien_ChiTietDao.countByCondition(
				Tbl_DKMH_DotDK_HocVien_ChiTiet.class, cons);
	}

	public final Tbl_DKMH_DotDK_HocVien_ChiTietDAO get_tbl_DKMH_DotDK_HocVien_ChiTietDao() {
		return _tbl_DKMH_DotDK_HocVien_ChiTietDao;
	}

	public final void set_tbl_DKMH_DotDK_HocVien_ChiTietDao(
			Tbl_DKMH_DotDK_HocVien_ChiTietDAO obj) {
		this._tbl_DKMH_DotDK_HocVien_ChiTietDao = obj;
	}

	public Tbl_DKMH_DotDK_HocVienDAO get_tbl_DKMH_DotDK_HocVienDao() {
		return _tbl_DKMH_DotDK_HocVienDao;
	}

	public void set_tbl_DKMH_DotDK_HocVienDao(
			Tbl_DKMH_DotDK_HocVienDAO _tbl_DKMH_DotDK_HocVienDao) {
		this._tbl_DKMH_DotDK_HocVienDao = _tbl_DKMH_DotDK_HocVienDao;
	}

	private Tbl_DKMH_DotDK_Nhom findNhom(int id) {
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("id");
			con.setValue1(new Long(id));
			con.setOperator("eq");
			cons.add(con);
		}
		List<String> columns = new ArrayList<String>();
		columns.add("id");

		List<Tbl_DKMH_DotDK_Nhom> nhom = _tbl_DKMH_DotDK_NhomnDao.findByCondition(Tbl_DKMH_DotDK_Nhom.class, cons, 1, 1);
		if(nhom==null||nhom.size()==0){
			return null;
		}
		return nhom.get(0);
	}
	
	private List<Tbl_ThongTin_HocVien> findHocVien(String ma) {
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("MSSV");
			con.setValue1(ma);
			con.setOperator("eq");
			cons.add(con);
		}
		List<String> columns = new ArrayList<String>();
		columns.add("id");

		List<Tbl_ThongTin_HocVien> hocvien = _tbl_ThongTin_HocVienDao.findByCondition(Tbl_ThongTin_HocVien.class, cons, 1, 1);
		return hocvien;
	}
	
	private List<Long> findDotDKHocVienId(String mssv , int dotDKId) {
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("hocVienId.MSSV");
			con.setValue1(mssv);
			con.setOperator("eq");
			cons.add(con);
		}
		{
			ConditionReport con = new ConditionReport();
			con.setCol("dotDKId.id");
			con.setValue1(new Long(dotDKId));
			con.setOperator("eq");
			cons.add(con);
		}
		List<String> columns = new ArrayList<String>();
		columns.add("id");
		List<Long> dotDkHocVienId = _tbl_DKMH_DotDK_HocVienDao.findColumnByCondition(Tbl_DKMH_DotDK_HocVien.class,cons, columns, 1, 1);
		
		return dotDkHocVienId;
	}
	
}// endclass

